package slash.client.swing.controls;

import javax.swing.*;

import slash.client.controllers.GameController;
import slash.game.Cell;
import slash.game.Board;
import slash.game.Game;
import slash.game.Terrain;
import slash.game.Entity;

import java.util.List;
import java.util.logging.Logger;
import java.awt.*;

/**
 * @author gsh
 */
public class MapPanel extends JPanel {
	protected static Logger logger = Logger.getLogger(MapPanel.class.getName());
	private Game game;

	public Game getGame() {
		return game;
	}

	public MapPanel(Game game) {
		this.game = game;
	}
	
	protected void paintComponent(Graphics g) {
		logger.info("painting");
		g.setColor(Color.BLACK);
		g.setFont(new Font("Monospaced", Font.PLAIN, 12));
		GameRender r = new GameRender(getGame());
		r.Render(g);
	}
	
	class GameRender {
		private Game game;
		public Game getGame() {
			return game;
		}

		public GameRender(Game game) {
			this.game = game;
		}
		
		public char RenderCell(Cell c) {
			List<Entity> contents = c.getContents();
			if (contents.isEmpty()) {
				// Render Terrain if Empty
				switch (c.getTerrain()) {
				case None:
					return '.';
				case Wall:
					return '+';
				case Rock:
					return '*';
				}
			} else {
				logger.info("Found contents: "+c);
				// Assume this is the player for now, render contents
				return '@';
			}
			return '?';
		}
		
		public void Render(Graphics g) {
			Board board = getGame().getBoard();
			int yoff = 20;
			int xoff = 10;
			for (int y = 0; y < board.getHeight(); y++) {
				StringBuilder str = new StringBuilder(board.getWidth());
				for (int x=0; x< board.getWidth(); x++) {
					str.append(RenderCell(board.getCell(x, y)));
				}
				g.drawChars(str.toString().toCharArray(), 0, board.getWidth(), xoff, yoff);
				yoff += 20;
			}
		}
		
	}
}
